package com.lingnan.tijian.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.tijian.constant.TijianConstant;
import com.lingnan.tijian.domain.Hospital;
import com.lingnan.tijian.domain.Orders;
import com.lingnan.tijian.domain.Setmeal;
import com.lingnan.tijian.domain.vo.OrderVO;
import com.lingnan.tijian.exception.BaseException;
import com.lingnan.tijian.mapper.OrderMapper;
import com.lingnan.tijian.service.IHospitalService;
import com.lingnan.tijian.service.IOrderService;
import com.lingnan.tijian.service.ISetmealService;
import com.lingnan.tijian.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 无Service业务层处理
 * 
 * @author manba
 * @date 2024-12-26
 */
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements IOrderService
{
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private IHospitalService hospitalService;

    @Override
    @Transactional
    public void insertOrder(Orders order) {
        //1.查询是否是否已预约
        List<Orders> list = lambdaQuery()
                .eq(Orders::getUserId, order.getUserId())
                .eq(Orders::getHpId, order.getHpId())
                .eq(Orders::getSmId, order.getSmId())
                .eq(Orders::getOrderDate, order.getOrderDate())
                .in(Orders::getState, TijianConstant.STATE_NOT_FILED)
                .list();
        if (!list.isEmpty()) {
            throw new BaseException("该时间段已预约");
        }
        order.setState(TijianConstant.STATE_NOT_FILED);
        save(order);
    }

    @Override
    @Transactional
    public List<OrderVO> selectOrderListByUserId() {
        Map<String, Object> map= ThreadLocalUtil.get();
        String userId = (String)map.get("userId");
        //1.获取预约列表
        List<Orders> list = lambdaQuery()
                .eq(Orders::getUserId, userId)
                .eq(Orders::getState, TijianConstant.STATE_NOT_FILED)
                .list();
        List<OrderVO> orderVOList = BeanUtil.copyToList(list, OrderVO.class);
        //2.根据套餐id 查询套餐名称
        //2.1提取套餐id
        List<Long> smIds = list.stream().map(Orders::getSmId).toList();
        //2.2 查询套餐名称
        List<Setmeal> setmealList = setmealService.lambdaQuery()
                .in(Setmeal::getId, smIds)
                .list();
        //2.3提取名字
        for (OrderVO orderVO : orderVOList) {
            for(Setmeal setmeal : setmealList){
                if (orderVO.getSmId().equals(setmeal.getId())){
                    orderVO.setSetmealName(setmeal.getName());
                }
            }
        }
        return orderVOList;
    }

    @Override
    public List<OrderVO> selectReportListByUserId() {
        Map<String, Object> map= ThreadLocalUtil.get();
        String userId = (String)map.get("userId");
        //1.获取预约列表
        List<Orders> list = lambdaQuery()
                .eq(Orders::getUserId, userId)
                .eq(Orders::getState, TijianConstant.STATE_FILED)
                .list();
        List<OrderVO> orderVOS = BeanUtil.copyToList(list, OrderVO.class);
        List<OrderVO> orderVOList = orderVOS;
        //2.根据套餐id 查询套餐名称
        //2.1提取套餐id
        List<Long> smIds = list.stream()
                .map(Orders::getSmId)
                .toList();
        if(smIds.isEmpty()){
            throw new BaseException("暂无预约记录");
        }
        //2.2 查询套餐名称
        List<Setmeal> setmealList = setmealService.lambdaQuery()
                .in(Setmeal::getId, smIds)
                .list();
        //2.3提取名字
        for (OrderVO orderVO : orderVOList) {
            for(Setmeal setmeal : setmealList){
                if (orderVO.getSmId().equals(setmeal.getId())){
                    orderVO.setSetmealName(setmeal.getName());
                }
            }
        }
        //2.4查询医院名称
        List<Hospital> hospitalList = hospitalService.lambdaQuery()
                .in(Hospital::getId, list.stream().map(Orders::getHpId).toList())
                .list();
        //2.5提取名字
        for (OrderVO orderVO : orderVOList) {
            for(Hospital hospital : hospitalList){
                if (orderVO.getHpId().equals(hospital.getId())){
                    orderVO.setHospitalName(hospital.getName());
                }
            }
        }
        return orderVOList;
    }

    /**
     * 取消预约
     * @param id
     */
    @Override
    public void cancelOrderByOrderId(Long id) {
        lambdaUpdate()
                .eq(Orders::getId, id)
                .set(Orders::getState, TijianConstant.STATE_CANCEL)
                .update();
    }
}
